export function generateDates (baseDate = new Date()) {
  const dates = []
  const startDate = new Date(baseDate)
  startDate.setHours(0, 0, 0, 0)

  for (let i = 0; i < 365; i++) {
    const date = new Date(startDate)
    date.setDate(date.getDate() + i)
    dates.push(date)
  }
  return dates
}

export function generateHours (selectedDate, minHour = 0) {
  const hours = []
  const startHour = selectedDate && selectedDate.getDate() === new Date().getDate()
    ? Math.max(minHour, new Date().getHours())
    : minHour

  for (let h = startHour; h < 24; h++) {
    hours.push(h)
  }
  return hours
}

export function generateMinutes (selectedDate, selectedHour, minMinute = 0) {
  const minutes = []
  let startMinute = 0

  if (selectedDate && selectedDate.getDate() === new Date().getDate() &&
        selectedHour === new Date().getHours()) {
    startMinute = Math.max(minMinute, Math.ceil(new Date().getMinutes() / 30) * 30)
  } else {
    startMinute = Math.max(minMinute, 0)
  }

  for (let m = startMinute; m < 60; m += 30) {
    minutes.push(m)
  }
  return minutes
}

export function validateTimeSelection (startDate, startTime, endDate, endTime) {
  const startTimestamp = new Date(
    startDate.getFullYear(),
    startDate.getMonth(),
    startDate.getDate(),
    startTime.hours,
    startTime.minutes
  ).getTime()

  const endTimestamp = new Date(
    endDate.getFullYear(),
    endDate.getMonth(),
    endDate.getDate(),
    endTime.hours,
    endTime.minutes
  ).getTime()

  if (endTimestamp <= startTimestamp) {
    alert('结束时间必须晚于开始时间')
    return false
  }
  return true
}
